﻿
@{
    ViewBag.Title = "选择权限";
    Layout = "~/Views/Shared/_LayoutBase.cshtml";
}
@section styles{

}
<div id="dMain">
    <div class="wrapper wrapper-content animated fadeInRight">
        <div class="ibox" style="margin-bottom: 0px;">
            <div>
                @using (Ajax.BeginForm("SelectReceiver", "Position", new AjaxOptions() { HttpMethod = "post", OnSuccess = "afterOk" }, new { area = "System", id = "datarightfrom", name = "datarightfrom" }))
                {
                    @Html.AntiForgeryToken()
                    <div id="toolbar">
                        <button type="button" class="btn btn-success" id="btn-check-all">全选</button>
                        <button type="button" class="btn btn-danger" id="btn-uncheck-all">全不选</button>
                    </div>
                    <div id="treeview-checkable" class=""></div>
                    <input id="Right" type="hidden" value="@ViewBag.right" />
                    <div class="from-footer">
                        <div class="btn-wrap" style="position: fixed;">
                            <a class="btn btn-sm btn-primary radius"  onclick="savefrom()">
                                <i class="iconfont icon-save"></i>
                                提交保存
                            </a>
                            <a class="btn btn-sm btn-warning radius" onclick="parent.layer.close(parent.layer.index);">
                                <i class="iconfont icon-fanhui"></i>
                                取消关闭
                            </a>
                        </div>
                    </div>
                }
            </div>
        </div>
    </div>
</div>
@section scripts{
    <script type="text/javascript" charset="utf-8" src="@Url.Content("~/Content/JS/common/jquery/jquery.unobtrusive-ajax.min.js")"></script>
    <script type="text/javascript" charset="utf-8" src="@Url.Content("~/Scripts/MicrosoftAjax.js")"></script>
    <script type="text/javascript" charset="utf-8" src="@Url.Content("~/Scripts/MicrosoftMvcAjax.js")"></script>
    <script type="text/javascript" charset="utf-8" src="@Url.Content("~/Content/JS/common/bootstrap/bootstrap-treeview.min.js")"></script>
    <script>
        function savefrom() {
            $("#datarightfrom").submit();
        }
        function afterOk(data) {
            parent.layer.close(parent.layer.index);
            var check = $('#treeview-checkable').treeview('getChecked');
            //$.each(check, function () {
            //    return $(this).nodeId;
            //});
            var data = {
                did_list:[],
                dtext_list:[],
                uid_list:[],
                utext_list:[]

            };
            $.each(check, function (i, o) {
                if (o.id.indexOf("D") >= 0) {
                    data.did_list.push(o.id);
                    data.dtext_list.push(o.text);
                } else {
                    data.uid_list.push(o.id);
                    data.utext_list.push(o.text);
                }
            });
            parent.setReceiver(data);
        }

    $(function () {
        $.getJSON('@Url.Action("GetjsonTreeData", "Send", new { area = "Document"})', { idstr: $('#Right').val() },function (data) {
            if (data != null) {
                $('#treeview-checkable').treeview({
                    data: data,
                    showIcon: true,
                    showCheckbox: true,
                    //multiSelect: true,// 不允许多选，因为我们要通过check框来控制
                    hierarchicalCheck: true,
                    onNodeChecked: nodeChecked,
                    onNodeUnchecked: nodeUnchecked

                });
            }
        })
        var nodeCheckedSilent = false;
        function nodeChecked(event, node) {
            if (nodeCheckedSilent) {
                return;
            }
            nodeCheckedSilent = true;
            checkAllParent(node);
            checkAllSon(node);
            nodeCheckedSilent = false;
        }

        var nodeUncheckedSilent = false;
        function nodeUnchecked(event, node) {
            if (nodeUncheckedSilent)
                return;
            nodeUncheckedSilent = true;
            uncheckAllParent(node);
            uncheckAllSon(node);
            nodeUncheckedSilent = false;
        }

        //选中全部父节点
        function checkAllParent(node) {
            $('#treeview-checkable').treeview('checkNode', node.nodeId, { silent: true });
            var parentNode = $('#treeview-checkable').treeview('getParent', node.nodeId);
            if (!("nodeId" in parentNode)) {
                return;
            } else {
                checkAllParent(parentNode);
            }
        }
        //取消全部父节点
        function uncheckAllParent(node) {
            $('#treeview-checkable').treeview('uncheckNode', node.nodeId, { silent: true });
            var siblings = $('#treeview-checkable').treeview('getSiblings', node.nodeId);
            var parentNode = $('#treeview-checkable').treeview('getParent', node.nodeId);
            if (!("nodeId" in parentNode)) {
                return;
            }
            var isAllUnchecked = true; //是否全部没选中
            for (var i in siblings) {
                if (siblings[i].state.checked) {
                    isAllUnchecked = false;
                    break;
                }
            }
            if (isAllUnchecked) {
                uncheckAllParent(parentNode);
            }

        }

        //级联选中所有子节点
        function checkAllSon(node) {
            $('#treeview-checkable').treeview('checkNode', node.nodeId, { silent: true });
            if (node.nodes != null && node.nodes.length > 0) {
                for (var i in node.nodes) {
                    checkAllSon(node.nodes[i]);
                }
            }
        }
        //级联取消所有子节点
        function uncheckAllSon(node) {
            $('#treeview-checkable').treeview('uncheckNode', node.nodeId, { silent: true });
            if (node.nodes != null && node.nodes.length > 0) {
                for (var i in node.nodes) {
                    uncheckAllSon(node.nodes[i]);
                }
            }
        }




        // Check/uncheck all
        $('#btn-check-all').on('click', function (e) {
            $('#treeview-checkable').treeview('checkAll', { silent: $('#chk-check-silent').is(':checked') });
        });

        $('#btn-uncheck-all').on('click', function (e) {
            $('#treeview-checkable').treeview('uncheckAll', { silent: $('#chk-check-silent').is(':checked') });
        });
        });

    </script>
}


